<?php
/**
 * Created by PhpStorm.
 * User: zhixuedong
 * Date: 2021/9/7
 * Time: 16:25
 */

namespace app\project\controller;

use app\common\controller\Common;
use app\index\controller\Base;
use app\project\service\PublicService;
use think\Controller;
use think\Db;
use think\Exception;
use think\Session;
use think\Validate;
class Help extends Controller
{

    //求助列表
    public function index(){
        $common = new Common();
        $page = empty($_GET['page']) ? 1 : $_GET['page'];
        $showPage = Session::get("page");
        $showPage = empty($showPage) ? 1 : $showPage;
        $this->assign("showPage", $showPage);
        Session::set("page", $page);
        //责任人
        $account = $common->public_select("project_account", ['status' => 1], 'id,name')->select();
        $this->assign("account", $account);
        //查询责任单位
        $project_dictionaries = $common->public_select("project_dictionaries", ['status' => 1], "id,name")->select();
        $this->assign("project_dictionaries", $project_dictionaries);
        if(!empty($_GET['account'])){
            $where['sumbit_id'] = $_GET['account'];
        }

        if(!empty($_GET['status'])){
            $where['status'] = $_GET['status'];
        }

        $status = [
            1=>"待回复",2=>"办理中",3=>"已办结",4=>"已评价",5=>"已关闭",6=>"已逾期",7=>"逾期办结"
        ];
        $this->assign("status", $status);
        if (!empty($_GET['type'])) {
            try{
                $limit = empty($_GET['limit']) ? 10 : $_GET['limit'];
                $start = ($page - 1) * $limit;
                $where['type'] = empty($_GET['istype'])?0:$_GET['istype'];
                $where['pid'] = 0;
                $list = $common->public_select("project_help",$where,null,$start,$limit)->order("id desc")->select();
                foreach ($list as $k=>$v){

                    if($v['type']==0){
                        $accountWhere['id'] = $v['sumbit_id'];
                        $list[$k]['name'] = $common->public_select("project_account",$accountWhere)->value('name');
                    }else{
                        $accountWhere['id'] = $v['sumbit_id'];
                        $list[$k]['name'] = $common->public_select("account",$accountWhere)->value('account');
                    }


                    $list[$k]['addtime'] = date("Y-m-d H:i:s",$v['addtime']);
                    $list[$k]['status'] = $status[$v['status']];
                }
                $count = $common->public_select("project_help",$where)->count("*");
                $black['code'] = 0;
                $black['data'] = $list;
                $black['count'] = $count;
                $black['page'] = $showPage;
            }catch (Exception $e){
                $black['code'] = 202;
                $black['msg'] = $e->getMessage();
            }
            return $black;
        }else{
            $istype = empty($_GET['istype'])?0:$_GET['istype'];
            $this->assign("istype", $istype);
            return $this->fetch("index");
        }

    }

    //删除操作
    public function del(){
        try {
            $common = new Common();
            $id = $_POST['id'];
            $list = $common->public_select("project_help", ['id' => ['in', $id]], "id")->select();
            $res = $common->public_select("project_help", ['id' => ['in', $id]])->delete();
            $common->public_select("project_help", ['pid' => ['in', $id]])->delete();
            $common->public_select("project_help_log", ['help_id' => ['in', $id]])->delete();
            if (empty($res)) {
                throw new Exception("删除失败");
            }
            foreach ($list as $k => $v) {
                $log['remark'] = "【工程管理】,账号【" . Session::get("user_name") . "】删除求助信息,关联id【" . $v['id'] . "】";
                $common->addLog($log);
            }
            $black['code'] = 0;
            $black['msg'] = "删除成功";
        } catch (Exception $e) {
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return $black;
    }

    //添加视图
    public function addView(){
        //获取所有项目
        $where['id'] = ['gt',0];
        $prolist = PublicService::ListService("project_pro",$where,"id,title");
        $assign['prolist'] = $prolist;
        $this->assign($assign);

        return $this->fetch("addView");
    }

    //查看求助详情
    public function detail(){
        $common = new Common();
        $id = $_GET['id'];

        $where['id'] = $id;
        $detail = $common->public_select("project_help",$where)->find();

        $detail['addtime'] = date("Y-m-d H:i:s",$detail['addtime']);

        //查询项目
        $detail['project'] = "";
        if(!empty($detail['pro_id'])){
            $proWhere['id'] = $detail['pro_id'];
            $detail['project'] = $common->public_select("project_pro",$proWhere)->value("title");
        }

        //查询协调人员信息
        $accountId = explode(",",$detail['accountId']);
        $accountId = array_filter($accountId);
        $accountWhere['id'] = ['in',$accountId];
        $coordinateList = $common->public_select("project_account",$accountWhere,"id,name,dictionariesId")->select();

        $status = [
            1=>"待回复",2=>"办理中",3=>"已办结",4=>"已评价",5=>"已关闭",6=>"已逾期",7=>"逾期办结"
        ];
        $this->assign('status',$status);
//        $detail['status'] = $status[$detail['status']];

        foreach ($coordinateList as $k=>$v){
            //查询责任单位
            $dictionariesWhere['id'] = $v['dictionariesId'];
            $dictionaries = $common->public_select("project_dictionaries",$dictionariesWhere)->value("name");
            $coordinateList[$k]['dictionaries'] = empty($dictionaries)?"":$dictionaries;
        }
        $this->assign('coordinateList',$coordinateList);
        $this->assign('detail',$detail);

        //查询沟通历史
        $historyWhere['pid'] = $id;
        $history = $common->public_select("project_help",$historyWhere)->order("id desc")->select();

        //查询回复人
        foreach ($history as $k=>$v){
            $accountWhere['id'] = $v['sumbit_id'];
            $coordinate = $common->public_select("project_account",$accountWhere,"id,name,dictionariesId")->find();
            $history[$k]['name'] = $coordinate['name'];
            $history[$k]['addtime'] = date("Y-m-d H:i:s",$v['addtime']);
        }

        $this->assign('history',$history);

        return $this->fetch("detail");
    }

    //更新内容
    public function save(){
        try {
            $common = new Common();
            $data = $_POST;
            if(!empty($data['accountId'])){
                $accountId = $data['accountId'];
                $data['accountId'] = ",".implode($accountId,",").",";
            }

            if(!empty($data['type'])){
                if($data['type']==1 & empty($data['id'])){
                    $data['sumbit_id'] = Session::get("id");
                }
            }


            if(!empty($data['id'])){
                $detail = $common->public_select("project_help",['id'=>$data['id']])->find();
                if($detail['status']==3){
                    throw new Exception("已办结，请勿重复操作");
                }
            }

            
            $data['sumbit_time'] = empty($data['sumbit_time'])?date("Y-m-d"):$data['sumbit_time'];

            $common->handle("project_help",$data);
            $black['code'] = 0;
            $black['msg'] = "操作成功";
        } catch (Exception $e) {
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return $black;
    }


    //删除协助人员
    public function delPersonnel(){
        try{
            $common = new Common();

            $account = $_POST['accountId'];

            $black['code'] = 0;
            $black['msg'] = "操作成功";
        }catch (Exception $e){
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return $black;
    }

    //添加协助人员
    public function doPersonnel(){
        try{
            $common = new Common();
            if(!empty($_POST['dictionarie'])){
                unset($where);
                $where['id'] = $_POST['dictionarie'];
                $dictionarie = $common->public_select("project_dictionaries",$where)->field("id,name")->find();
            }

            if(!empty($_POST['account'])){
                unset($where);
                $where['id'] = $_POST['account'];
                $account = $common->public_select("project_account",$where)->field("id,name")->find();
            }

            if(empty($_POST['account'])){
               throw new Exception("请选择人员");
            }

            $html = '<span class="zxd_coordinate">'.$dictionarie['name'].'【'.$account['name'].'】<input type="hidden" name="accountId[]" value="'.$account['id'].'"> <font class="del" >x</font></span>';
            $black['code'] = 0;
            $black['html'] = $html;
            $black['msg'] = "操作成功";
        }catch (Exception $e){
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return $black;
    }

    //添加协助人视图
    public function addPersonnel(){
        $common = new Common();
        //获取所有单位
        unset($where);
        $where['status'] = 1;
        $dictionarieList = $common->public_select("project_dictionaries",$where)->field("id,name")->select();

        //获取所有人员
        unset($where);
        $where['status'] = 1;
        $accountList = $common->public_select("project_account",$where)->field("id,name")->select();

        $this->assign('dictionarieList',$dictionarieList);
        $this->assign('accountList',$accountList);
        return $this->fetch("addPersonnel");
    }

    //查询部门下的所有人员
    public function personnelApi(){
        try{
            $common = new Common();
            $where['dictionariesId'] = $_POST['dictionariesId'];
            $where['status'] = 1;
            $list = $common->public_select("project_account",$where)->field("id,name")->select();
            $black['code'] = 0;
            $black['list'] = $list;
        }catch (Exception $e){
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return $black;
    }


    //编辑视图
    public function editView(){
        $common = new Common();
        $id = $_GET['id'];

        $where['id'] = $id;
        $detail = $common->public_select("project_help",$where)->find();

        $detail['addtime'] = date("Y-m-d H:i:s",$detail['addtime']);

        //查询项目
        $detail['project'] = "";
        if(!empty($detail['pro_id'])){
            $proWhere['id'] = $detail['pro_id'];
            $detail['project'] = $common->public_select("project_pro",$proWhere)->value("title");
        }

        //查询协调人员信息
        $accountId = explode(",",$detail['accountId']);
        $accountId = array_filter($accountId);
        $accountWhere['id'] = ['in',$accountId];
        $coordinateList = $common->public_select("project_account",$accountWhere,"id,name,dictionariesId")->select();
        foreach ($coordinateList as $k=>$v){
            //查询责任单位
            $dictionariesWhere['id'] = $v['dictionariesId'];
            $dictionaries = $common->public_select("project_dictionaries",$dictionariesWhere)->value("name");
            $coordinateList[$k]['dictionaries'] = empty($dictionaries)?"":$dictionaries;
        }
        $this->assign('coordinateList',$coordinateList);


        $status = [
            1=>"待回复",2=>"办理中",3=>"已办结",4=>"已评价",5=>"已关闭",6=>"已逾期",7=>"逾期办结"
        ];
        $this->assign('status',$status);
        $this->assign('detail',$detail);

        //获取所有项目
        $where['id'] = ['gt',0];
        $prolist = PublicService::ListService("project_pro",$where,"id,title");
        $assign['prolist'] = $prolist;
        $this->assign($assign);

        return $this->fetch("editView");
    }


}